package org.nepu.scr.indexrate.raterisk.riskscore;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Validator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lboot.core.domain.ResponseDTO;
import org.lboot.core.exception.BusinessException;
import org.lboot.jpa.utils.JpaDataUtil;
import org.nepu.scr.indexrate.raterisk.riskscore.params.RiskScoreData;
import org.nepu.scr.indexrate.raterisk.riskscore.params.RiskScoreUpdateParams;
import org.nepu.scr.indexrate.raterisk.prerisk.params.RiskCreateParams;
import org.nepu.scr.indexrate.raterisk.prerisk.params.RiskQueryParams;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("score")
@AllArgsConstructor
@Api(tags="作业前风险评级比较矩阵")
public class RiskScoreController {

    RiskScoreService riskService;



//    @AuthLogin
//    @PostMapping("risks")
//    @ApiOperation(value = "新建指标比较矩阵")
    public ResponseDTO<Object> dataCreate(@Validated @RequestBody RiskCreateParams params){



        RiskScoreData data = new RiskScoreData();
        List<RiskScore> dataList = riskService.getByRiskName(params.getRiskId());



        if (dataList.size() > 0){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"存在指标评价任务"+params.getRiskId());
        }

        RiskScore risk = new RiskScore();
        risk.setRiskName(params.getRiskId());
        data.setRiskId(params.getRiskId());
        RiskScore e = riskService.saveOne(risk);
        String riskDataId = e.getId();
        riskService.saveRiskData(riskDataId, data);

        return ResponseDTO.succData(
                e
        );
    }

//    @DeleteMapping("risks/{id}")
//    @ApiOperation(value = "删除指标比较矩阵")
    public ResponseDTO<Object> dataDelete(@PathVariable("id") String id){
        riskService.deleteRiskData(id);
        riskService.deleteOne(id);
        return ResponseDTO.succMsg("删除成功");
    }

    @GetMapping("risks")
    @ApiOperation(value = "指标比较矩阵列表")
    public ResponseDTO<Object> dataList(RiskQueryParams params){
        Pageable pageable = JpaDataUtil.buildPageable(params);
        if (Validator.isEmpty(params.getSearchKey())){
            RiskScore data = new RiskScore();
            BeanUtil.copyProperties(params,data, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
            Example<RiskScore> example = Example.of(data);
            return ResponseDTO.succData(
                    riskService.getAll(pageable,example)
            );
        }else {
            Specification<RiskScore> spec = (root, query, cb) -> {
                List<Predicate> list = new ArrayList<>();
                Predicate p1 = cb.like(root.get("riskId").as(String.class),"%"+params.getSearchKey()+"%");
                list.add(p1);
                Predicate[] predicates = new Predicate[list.size()];
                return cb.or(list.toArray(predicates));
            };
            return ResponseDTO.succData(
                    riskService.getAll(pageable, spec)
            );
        }
    }

    @GetMapping("risks/{id}")
    @ApiOperation(value = "比较矩阵查询")
    public ResponseDTO<Object> dataQuery(@PathVariable("id") String id){
        RiskScoreData riskData = riskService.readRiskData(id);
        return ResponseDTO.succData(
                riskData
        );
    }

    @PutMapping("risks/{riskId}/data")
    @ApiOperation(value = "比较矩阵更新")
    public ResponseDTO<Object> dataUpdate(@PathVariable("riskId") String riskId, @Validated @RequestBody RiskScoreUpdateParams params){
        RiskScoreData data = riskService.readRiskData(riskId);
        BeanUtil.copyProperties(params,data, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return ResponseDTO.succData(
                riskService.saveRiskData(riskId, data)
        );
    }
}
